﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Threading;
using CooStationsToolsNew.Model;

namespace CooStationsToolsNew
{
    /// <summary>
    /// ProgressWin.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressWin : Window
    {
        ProjectTools pt = new ProjectTools();
        public ProgressWin()
        {
            InitializeComponent();
            NewTimer();
            SaveNow();
        }


        /// <summary>
        /// 进度条的值
        /// </summary>
        /// <param name="pvalue">进度条的值</param>
        public void ChangeProgressValue(double pvalue)
        {
            proBar.Value = pvalue;
        }

        private void NewTimer()
        {
            System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 500;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Enabled = true;
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            count++;
            ChangeProgressValue(count);
            if (thcopy.IsAlive == false)
            {
                this.Close();
            }
        }
        int count = 0;
        Thread thcopy;

        private void SaveNow()
        {
            thcopy = new Thread(Save);
            thcopy.Start();
        }

        private void Save()
        {
            try
            {
                for (int i = 0; i < App.Pagelist.Count; i++)
                {
                    CfyPage cp = (CfyPage)App.Pagelist[i];
                    pt.SaveDtToXml(App.appPath + "\\temp\\" + cp.PageName + ".xml");
                }
                count = 30;
                if (!Directory.Exists(App.proPath + "\\pic"))
                {
                    Directory.CreateDirectory(App.proPath + "\\pic");
                }

                DirectoryInfo dr = new DirectoryInfo(App.appPath + "\\temp");
                int n = dr.GetFiles("*.*", SearchOption.AllDirectories).Length;

                CopyFile(App.appPath + "\\temp", App.proPath);
                count = 70;
                for (int i = 0; i < App.Pagelist.Count; i++)
                {
                    CfyPage cp = (CfyPage)App.Pagelist[i];
                    pt.RemoveNameXml(App.proPath + "\\" + cp.PageName + ".xml");
                }
                count = 100;
                Thread.Sleep(1000);
                
                thcopy.Abort();
            }
            catch
            {

            }
        }

        /// <summary>
        /// 文件夹复制
        /// </summary>
        /// <param name="sourceDirName">原始路径</param>
        /// <param name="destDirName">目标路径</param>
        /// <returns></returns>
        public void CopyFile(string sourceDirName, string destDirName)
        {
            try
            {
                if (sourceDirName.Substring(sourceDirName.Length - 1) != "\\")
                {
                    sourceDirName = sourceDirName + "\\";
                }
                if (destDirName.Substring(destDirName.Length - 1) != "\\")
                {
                    destDirName = destDirName + "\\";
                }
                if (Directory.Exists(sourceDirName))
                {
                    if (!Directory.Exists(destDirName))
                    {
                        Directory.CreateDirectory(destDirName);
                    }
                    foreach (string item in Directory.GetFiles(sourceDirName))
                    {
                        try
                        {
                            File.Copy(item, destDirName + System.IO.Path.GetFileName(item), true);

                        }
                        catch { }
                    }
                    foreach (string item in Directory.GetDirectories(sourceDirName))
                    {
                        CopyFile(item, destDirName + item.Substring(item.LastIndexOf("\\") + 1));
                    }
                }
            }
            catch (Exception)
            {

            }
        }
    }
}
